package com.yonyou.pmclouds.task.util;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseVO;
import com.yonyou.pmclouds.enterprise.rmiitf.EnterpriseQuery;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogBillStatus;
import com.yonyou.pmclouds.task.entity.TaskVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TaskUtil {

    private static final String[] preFix = {"您已审批通过了“", "您已驳回了“"};

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private EnterpriseQuery enterpriseQuery;

    public String getTaskDescribe(TaskVO vo) throws BusinessException {

        int index = vo.getBillstatus() == BillStatusConst.COMMON_REJECT_STATUS ? 1 : 0;

        switch (vo.getSrc_bill_type()) {
            case BillTypeConst.PROBLEMLOG_BILLTYPE:  //问题记录，问题原因名称
                vo.setMessage(getMessageForProblem(vo, BillTypeConst.PROBLEMLOG_BILLTYPE));
                break;
            case BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE: // 安全整改，问题原因名称
                vo.setMessage(getMessageForProblem(vo, BillTypeConst.SAFETY_RECTIFICATION_BILLTYPE));
                break;
            case BillTypeConst.WORKCONNECT_BILLTYPE:  //工作联系，标题
                vo.setMessage("您已确认接收“" + vo.getSender_name() + "”下发的[" + (vo.getBillDescribe() == null ? "工作联系" : vo.getBillDescribe()) + "]的工作联系");
                break;
            case BillTypeConst.PROCEDUREINSPECT_BILLTYPE:  //工序报验，工序名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "工序报验" : vo.getBillDescribe()) + "]的工序报验");
                break;
            case BillTypeConst.ALTER_AND_VISA_BILLTYPE:  //签证申请，签证类别名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "签证申请" : vo.getBillDescribe()) + "]的签证申请");
                break;
            case BillTypeConst.COMPREHENSIVE_ACCEPTANCE_BILLTYPE:  //综合验收，施工单位名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (StringUtils.isEmpty(vo.getBillDescribe()) ? "综合验收" : vo.getBillDescribe()) + "]的综合验收");
                break;
            case BillTypeConst.CONSTRUCTIONLOG_BILLTYPE:  //施工记录，检查标准名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "施工记录" : vo.getBillDescribe()) + "]的施工记录");
                break;
            case BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE:  //施工日志，制单日期
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "施工日志" : vo.getBillDescribe()) + "]的施工日志");
                break;
            case BillTypeConst.PROCESSPHOTO_BILLTYPE:  //工序拍照，工序名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "工序拍照" : vo.getBillDescribe()) + "]的工序拍照");
                break;
            case BillTypeConst.PROCESS_PLAN_BILLTYPE:  //进度计划，计划名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "进度计划" : vo.getBillDescribe()) + "]的进度计划");
                break;
            case BillTypeConst.ALTER_BILLTYPE:  //变更申请，变更类别名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "变更申请" : vo.getBillDescribe()) + "]的变更申请");
                break;
            case BillTypeConst.WORKSTART_BILLTYPE:  //开工报告，施工单位名称
                EnterpriseVO[] enterpriseVOS = enterpriseQuery.queryByIds(new String[]{vo.getBillDescribe()});
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null|| (enterpriseVOS==null ||enterpriseVOS.length ==0) ? "开工报告" : enterpriseVOS[0].getName()) + "]的开工报告");
                break;
            case BillTypeConst.PROCESS_WRITE_BILLTYPE:  //计划填报，计划名称
                vo.setMessage(preFix[index] + vo.getSender_name() + "”提交的[" + (vo.getBillDescribe() == null ? "计划填报" : vo.getBillDescribe()) + "]的计划填报");
                break;
            case BillTypeConst.WORKSTOP_BILLTYPE://停工单
                vo.setMessage(preFix[index] + vo.getSender_name() + "提交的[" + vo.getProject_name()+ "的" + (vo.getBillDescribe() == null ? "停工单" : vo.getBillDescribe()));
                break;
            case BillTypeConst.REPEATWORK_BILLTYPE://复工单
                vo.setMessage(preFix[index] + vo.getSender_name() + "提交的[" + vo.getProject_name()+ "的" + (vo.getBillDescribe() == null ? "复工单" : vo.getBillDescribe()));
                break;
        }
        return "";
    }

    /**
     * 获取问题记录、安全整改的提示消息
     *
     * @param vo
     * @param billType
     * @return
     */
    private  String getMessageForProblem(TaskVO vo, String billType) {
        String message = "";
        String[] describes = null;
        if (vo.getBillDescribe() != null)
            describes = vo.getBillDescribe().split("\n");

        String msg = "";
        String billName;
        if (BillTypeConst.PROBLEMLOG_BILLTYPE.equals(billType)) {
            billName = "问题记录";
        } else {
            billName = "安全整改";
        }

        if (describes != null && describes.length > 0 && describes[0] != null) {
            String[] msgs = describes[0].split("：");
            if (msgs != null && msgs.length > 1 && msgs[1] != null)
                msg = msgs[1];
        }

        if (StringUtils.isEmpty(msg))
            msg = billName;

        if (vo.getBillstatus() == ProblemLogBillStatus.STATUS_ACCEPTING) {
            message = "您已整改了“" + vo.getSender_name() + "”提交的[" + msg + "]的" + billName;
        } else if (vo.getBillstatus() == ProblemLogBillStatus.STATUS_REPAIRING) {
            message = "您已驳回了“" + vo.getSender_name() + "”提交的[" + msg + "]的" + billName;
        } else if (vo.getBillstatus() == ProblemLogBillStatus.STATUS_END) {
            message = "您已验收通过了“" + vo.getSender_name() + "”提交的[" + msg + "]的" + billName;
        }

        return message;
    }

}
